using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

using Verety.SharedMeta.MessagingServiceMeta;
using Verety.SharedMeta.MessagingServiceMeta.Contacts;
using Verety.SharedMeta.MessagingServiceMeta.Conversation;

using Verety.SharedMeta.DirectoryServiceMeta;
using Verety.SharedMeta.DirectoryServiceMeta.Contacts;
using Verety.SharedMeta.DirectoryServiceMeta.Contacts.ColumnNames;

using SBPweb.Presentation.Windows.Forms.Controls;
using SBPweb.Modules.Modularity;

namespace SBPweb.Modules.ChatController.Controls
{ 
    public partial class PartnerSelectControl : ModalDockingUserControl
    {
        private IDirectoryService mDirectoryService;
		private ChatControl mChatControl;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        public PartnerSelectControl(IModule parent, ChatControl chatControl, bool showOffline)
            : base(parent)
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            Dock = DockStyle.Fill;
            MinimumSize = new System.Drawing.Size(400, 400);
            BorderStyle = SBPweb.Modules.Smartparts.BorderStyle.SizableToolWindow;
            OkB.Click += new EventHandler(OkB_Click);
            CancelB.Click += new EventHandler(CancelB_Click);

			mChatControl = chatControl;
            mDirectoryService = mChatControl.Conversation.Messenger as IDirectoryService;

            DirectoryContactsDataset dcd = mDirectoryService.Contacts.DataSet as DirectoryContactsDataset;
			ContactsCBL.Items.Clear();
			ContactsCBL.Sorted = false;
			foreach (DirectoryContactsDataset.ContactDetailsRow row in (mDirectoryService.Contacts.DataSet as DirectoryContactsDataset).ContactDetails.Rows)
			{
				if (row.RowState != DataRowState.Deleted && row.RowState != DataRowState.Detached)
				{
					string userName = (mDirectoryService.ItemFactory.Create<IContactId>(row[ContactDetailTableColumnNames.FullJID].ToString()) as IContactId).User;
					string resource = (mDirectoryService.ItemFactory.Create<IContactId>(row[ContactDetailTableColumnNames.FullJID].ToString()) as IContactId).Resource;
					if ((!(mDirectoryService.Contacts.DataSet as DirectoryContactsDataset).HiddenJIDs.Contains(userName)) && (mDirectoryService.Connection.UserName != userName || mDirectoryService.Connection.Resource != resource))
					{
						if (showOffline || resource != Verety.SharedMeta.DirectoryServiceMeta.Enums.StringConstants.Resources.UnknownResource)
						{
							ContactsCBL.Items.Add(row.FullJID);
						}
					}
				}
			}
			ContactsCBL.Sorted = true;
        }

        void CancelB_Click(object sender, EventArgs e)
        {
            IsVisible = false;
        }

        void OkB_Click(object sender, EventArgs e)
        {
            (mChatControl.Conversation as IConference).Invite(SelectedContacts);
            IsVisible = false;
        }

        #region IPartnerSelectControl Members

        public ICollection<IContactId> SelectedContacts
        {
            get
            {
                List<IContactId> selectedContacts = new List<IContactId>();

                foreach (string jid in ContactsCBL.CheckedItems)
                {
					selectedContacts.Add(mDirectoryService.ItemFactory.Create<IContactId>(jid));
                }

                return selectedContacts.ToArray();
            }
        }

        #endregion
    }
}
